package com.rongxun.hbwallet.mvp.model;

import android.content.Context;

import com.rongxun.base.BaseModel;
import com.rongxun.hbwallet.constants.HttpConstants;
import com.rongxun.hbwallet.model.Response;
import com.rongxun.hbwallet.model.bean.CandyTask;
import com.rongxun.hbwallet.model.bean.ImageCode;
import com.rongxun.hbwallet.model.bean.Notice;
import com.rongxun.hbwallet.model.bean.Token;
import com.rongxun.hbwallet.model.bean.User;
import com.rongxun.hbwallet.model.bean.UserCandyInfo;
import com.rongxun.hbwallet.model.bean.UserCitymoneyInfo;
import com.rongxun.hbwallet.mvp.contract.ICodeLogin;
import com.rongxun.hbwallet.mvp.contract.ICreateWallet;
import com.rongxun.hbwallet.mvp.contract.IForget;
import com.rongxun.hbwallet.mvp.contract.IHome;
import com.rongxun.hbwallet.mvp.contract.ILoadWallet;
import com.rongxun.hbwallet.mvp.contract.ILogin;
import com.rongxun.hbwallet.mvp.contract.IMain;
import com.rongxun.hbwallet.mvp.contract.IMenttionMeesage;
import com.rongxun.hbwallet.mvp.contract.IMySelf;
import com.rongxun.hbwallet.mvp.contract.IRegister;
import com.rongxun.hbwallet.mvp.contract.ITransfer;
import com.rongxun.hbwallet.mvp.contract.IWalletDetail;
import com.rongxun.hbwallet.mvp.contract.Iwithdraw;
import com.rongxun.http.Api;
import com.rongxun.http.ApiProvider;
import com.rongxun.utils.JsonUtils;
import com.rongxun.utils.RxUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 创建者     庄丰泽
 * 创建时间   2018/5/18 13:45
 * 描述	      网络请求实现类
 * <p>
 */


public  class ApiModel extends BaseModel implements IWalletDetail.IModel, IMain.IModel, IMenttionMeesage.IModel, ICodeLogin.IModel, IRegister.IModel, IForget.IModel, ILogin.IModel, IHome.IModel, ICreateWallet.IModel, ILoadWallet.IModel,IMySelf.IModel,Iwithdraw.IModel,ITransfer.IModel{


    private static final String TAG     = "ApiModel";
    private static final String BaseUrl = HttpConstants.BASE_URL;


    private final ApiProvider mProvider;

    public ApiModel(Context pContext) {
        super(pContext);
        mProvider = Api.with(BaseUrl);
    }

    @Override
    protected void innerRelease() {

    }

    @Override
    public Observable<Response<ImageCode>> checkImgVerificationCode() {
        return mProvider.getApi().checkImgVerificationCode()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

    }

    @Override
    public Observable<Response<User>> getMobileCode(RequestBody route) {
        return mProvider.getApi().getMobileCode(route)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<Response<User>> findPassword(RequestBody route) {
        return mProvider.getApi().findPassword(route)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<Response<User>> Rigister(RequestBody route) {
        return mProvider.getApi().Rigister(route)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Observable<Response<User>> Login(RequestBody route) {
        return mProvider.getApi().login(route)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Flowable<Response<Notice>> getNotice(String language) {
        return mProvider.getApi().getNotice(language).compose(RxUtil.rxSchedulerHelper());
    }

    @Override
    public Flowable<Response<List<CandyTask>>> getCandyTask() {
        return mProvider.getApi().getCandyTask().compose(RxUtil.rxSchedulerHelper());
    }

    @Override
    public Flowable<Response<Token>> refreshToken() {
        return mProvider.getApi().refreshToken().compose(RxUtil.rxSchedulerHelper());
    }

    @Override
    public Flowable<Response<String>> saveWallet(String userId, String address) {
        Map<String, String> map = new HashMap<>();
        map.put("uid", userId);
        map.put("address", address);
        String toJson = JsonUtils.mapToJson(map);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), toJson);

        return mProvider.getApi().saveWallet(requestBody).compose(RxUtil.rxSchedulerHelper());
    }

    @Override
    public Flowable<Response<String>> deleteWallet(String walletId) {
        String json = "[\"" + walletId + "\"]";
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), json);

        return mProvider.getApi().deleteWallet(requestBody).compose(RxUtil.rxSchedulerHelper());
    }


    @Override
    public Observable<Response<String>> getConfig(String key_value) {
        return mProvider.getApi().getConfig(key_value)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Flowable<Response<UserCitymoneyInfo>> getUserCityMoneyInfo() {
        return  mProvider.getApi().getUserCityMoneyInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

    @Override
    public Flowable<Response<UserCandyInfo>> getUserCandyInfo() {
        return mProvider.getApi().getUserCandyInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());

    }

    @Override
    public Observable<Response<User>> getDealMobileCode() {
        return mProvider.getApi().getDealMobileCode()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
    @Override
    public Observable<Response<User>> ChangeSweet(int chang_number) {
        return mProvider.getApi().ChangeSweet(chang_number)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }
}


